import { Component, OnInit } from '@angular/core';

// 1.类装饰器;
function Greeter(greeting: string) {
  return function(target: Function) {
    target.prototype.greet = function(): void {
      console.log(greeting);
    };
  };
}

@Greeter('你好啊')
class Greeting {
  mgs: string;

  constructor() {
    // 内部实验;
  }
}
const myGreeting = new Greeting();
// myGreeting.greet(); // console output: '您好!';


// 2.属性装饰器声明：
function LogChanges(callbackObject: any) {
  return function(target: Object, key: string): void {
    let propertyValue: string = this[key];
      if (delete this[key]) {
        Object.defineProperty(target, key, {
          get: function () {
              return propertyValue;
          },
          set: function(newValue) {
              propertyValue = newValue;
              callbackObject.onchange.call(this, propertyValue);
          }
     });
    }
  };
}

// class Fruit {
//   @LogChanges({
//     onchange: function(newValue: string): void {
//       console.log(`The fruit is ${newValue} now`);
//     }
//   })
//   name: string;
// }

// const fruit = new Fruit();
// console.log(fruit);
// fruit.name = 'apple'; // console output: 'The fruit is apple now'
// fruit.name = 'banana'; // console output: 'The fruit is banana now'



@Component({
  selector: 'app-test-pages',
  templateUrl: './test-pages.component.html',
  styleUrls: ['./test-pages.component.scss']
})
export class TestPagesComponent implements OnInit {

  constructor() { }

  ngOnInit() {
    // const myGreeting = new Greeting();
    // myGreeting.greet();
  }

}
